<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="任务类型">
              <el-select
                v-model="query.tasktype"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in tasktypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="任务内容">
              <el-input
                v-model="query.taskcontent"
                placeholder="请输入"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="责任人">
              <user-select
                v-model="query.dutyperson"
                placeholder="请选择
                                        "
                clearable
              />
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="6">
              <el-form-item label="开始时间">
                <el-date-picker
                  v-model="query.starttime"
                  type="datetimerange"
                  value-format="timestamp"
                  format="yyyy-MM-dd HH:mm:ss"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <!-- <el-button
              type="primary"
              icon="el-icon-plus"
              v-has="'btn_add'"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button
              type="text"
              icon="el-icon-download"
              @click="exportData()"
              v-has="'btn_download'"
              >导出
            </el-button> -->
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JNPF-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull isContainer />
          </div>
        </div>
        <JNPF-table
          v-loading="listLoading"
          :data="list"
          :cell-style="tableRowStyleName"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="tasktype"
            label="任务类型"
            width="100"
            align="left"
          />
          <el-table-column
            prop="taskcontent"
            label="任务内容"
            width="0"
            align="left"
          />
          <el-table-column
            prop="starttime"
            label="开始时间"
            width="135"
            align="left"
          />
          <el-table-column
            prop="endtime"
            label="结束时间"
            width="135"
            align="left"
          />
          <el-table-column
            prop="projecttime"
            label="工期时间"
            width="100"
            align="left"
          />
          <el-table-column
            prop="taskendtime"
            label="任务完结时间"
            width="135"
            align="left"
          />
          <el-table-column
            prop="operatorName"
            label="执行人"
            width="100"
            align="left"
          />
          <el-table-column
            prop="taskstatusName"
            label="任务状态"
            width="100"
            align="left"
          />
          <el-table-column
            prop="dutypersonName"
            label="责任人"
            width="0"
            align="left"
          />
          <el-table-column
            prop="dutydepartment"
            label="责任部门"
            width="0"
            align="left"
          />
          <el-table-column
            prop="dutyleader"
            label="责任领导"
            width="0"
            align="left"
          />
          <el-table-column label="操作" fixed="right" width="70">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="viewTaskLog(scope.row.id)"
                v-has="'btn_detail'"
                >详情
              </el-button>

              <!-- <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id)"
                v-has="'btn_edit'"
                :disabled="scope.row.taskstatus != '1'"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="JNPF-table-delBtn"
                v-has="'btn_remove'"
                :disabled="scope.row.taskstatus != '1'"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
              <el-button
                type="text"
                v-has="'btn_detail'"
                @click="goDetail(scope.row.id)"
                >详情
              </el-button>

              <el-dropdown hide-on-click>
                <span class="el-dropdown-link">
                  <el-button type="text" size="mini"
                    >{{ $t("common.moreBtn")
                    }}<i class="el-icon-arrow-down el-icon--right"></i>
                  </el-button>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    @click.native="xfZjTask(scope.row.id,2)"
                    v-has="'btn_xiafa'"
                    :disabled="
                      scope.row.focusman != '' && scope.row.focusman != null
                    "
                    >下发</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="xfzjTask(scope.row.id,2)"
                    v-has="'btn_xiafa'"
                    :disabled="
                      ['1', '4'].indexOf(scope.row.taskstatus) <0 || scope.row.dutyperson != currentUserId
                    "
                    >下发</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="xfzjTask(scope.row.id,3)"
                    v-has="'btn_zj'"
                    :disabled="
                      ['2', '3'].indexOf(scope.row.taskstatus) <0 || scope.row.operator != currentUserId
                    "
                    >转交</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="updateTaskStatus(scope.row.id, 6)"
                    v-has="'btn_jf'"
                    :disabled="
                      scope.row.taskstatus != '5' || scope.row.operator != currentUserId
                    "
                    >交付</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="ccTask(scope.row.id)"
                    v-has="'btn_cc'"
                    :disabled="['2', '3', '5'].indexOf(scope.row.taskstatus) <0 || scope.row.dutyperson != currentUserId "
                    >催促</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="tqTask(scope.row.id)"
                    v-has="'btn_tq'"
                    :disabled="scope.row.taskstatus != '6' || scope.row.dutyperson != currentUserId "
                    >调期</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="updateTaskStatus(scope.row.id, 5)"
                    v-has="'btn_js'"
                    :disabled="
                      ['2', '3'].indexOf(scope.row.taskstatus) <0 || scope.row.operator != currentUserId
                    "
                    >接受</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="updateTaskStatus(scope.row.id, 4)"
                    v-has="'btn_jj'"
                    :disabled="
                      ['2', '3'].indexOf(scope.row.taskstatus) <0 || scope.row.operator != currentUserId
                    "
                    >拒绝</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="viewTaskLog(scope.row.id)"
                    v-has="'btn_detail'"
                    >详情</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown> -->
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <!-- <JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" />
    <Cc-Form v-if="formVisibleCc" ref="CcForm" @refresh="refresh" />
    <Tq-Form v-if="formVisibleTq" ref="TqForm" @refresh="refresh" />
    <XfZj-Form v-if="formVisibleXfzj" ref="XfZjForm" @refresh="refresh" /> -->
    <ViewTaskLogForm v-if="formVisibleLog" ref="ViewTaskLogForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex"
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
// import JNPFForm from "./Form";
// import CcForm from "./CcForm";
// import TqForm from "./TqForm";
// import XfZjForm from "./XfZjForm";
import ViewTaskLogForm from "./ViewTaskLog";
import ExportBox from "./ExportBox";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import Detail from "./Detail";

export default {
  components: { ViewTaskLogForm, ExportBox, Detail},
  data() {
    return {
      currentUserId:"",
      showAll: false,
      detailVisible: false,
      query: {
        tasktype: undefined,
        taskcontent: undefined,
        dutyperson: undefined,
        starttime: undefined,
        taskstatus: "6"
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime",
      },
      formVisible: false,
      // formVisibleCc: false,
      // formVisibleTq: false,
      // formVisibleXfzj: false,
      formVisibleLog: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "tasktype", label: "任务类型" },
        { prop: "taskcontent", label: "任务内容" },
        { prop: "starttime", label: "开始时间" },
        { prop: "endtime", label: "结束时间" },
        { prop: "projecttime", label: "工期时间" },
        { prop: "taskendtime", label: "任务完结时间" },
        { prop: "operator", label: "执行人" },
        { prop: "taskstatus", label: "任务状态" },
        { prop: "dutyperson", label: "责任人" },
        { prop: "dutydepartment", label: "责任部门" },
        { prop: "dutyleader", label: "责任领导" },
      ],
      tasktypeOptions: [],
      tasktypeProps: { label: "fullName", value: "enCode" },
      taskstatusOptions: [],
      taskstatusProps: { label: "fullName", value: "enCode" },
    //   detailOptions: {
    //         pageSize: "5",
    //         pagination: true, // 是否分页
    //         sortable: true, // 是否启用排序
    //         rowStyle:rowStyle,
    //     }
    };
  },

  computed: {
    ...mapGetters(['userInfo']),
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },

  created() {
    this.initData();
    this.gettasktypeOptions();
  },
  methods: {
      tableRowStyleName({row}) {
          if(row.rowBgColor=='1'){
                  return 'background-color: #FBA0A0;';
          }else if(row.rowBgColor=='2'){
            return 'background-color: yellow;';
          }
      },
    gettasktypeOptions() {
      getDictionaryDataSelector("316926148233659781").then((res) => {
        this.tasktypeOptions = res.data.list;
      });
    },
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.currentUserId = this.userInfo.userId;
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/example/BusTask/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        console.log(_list);
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    // handleDel(id) {
    //   this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
    //     type: "warning",
    //   })
    //     .then(() => {
    //       request({
    //         url: `/api/example/BusTask/${id}`,
    //         method: "DELETE",
    //       }).then((res) => {
    //         this.$message({
    //           type: "success",
    //           message: res.msg,
    //           onClose: () => {
    //             this.initData();
    //           },
    //         });
    //       });
    //     })
    //     .catch(() => {});
    // },

    // ccTask(id) {
    //     this.formVisibleCc = true;
    //     this.$nextTick(() => {
    //     this.$refs.CcForm.init(id, true);
    //   });
    // },
    
    // tqTask(id) {
    //     this.formVisibleTq = true;
    //     this.$nextTick(() => {
    //     this.$refs.TqForm.init(id, true);
    //   });
    // },

    // xfzjTask(id,taskStatus) {
    //     this.formVisibleXfzj = true;
    //     this.$nextTick(() => {
    //     this.$refs.XfZjForm.init(id,taskStatus,true);
    //   });
    // },

    // updateTaskStatus(id,taskStatus){
    //   var msg=taskStatus==6?"完成":taskStatus==5?"接受":"拒绝";
    //   this.$confirm("是否"+msg+"任务?", "提示", {
    //     type: "warning",
    //   })
    //     .then(() => {
    //       request({
    //         url: `/api/example/BusTask/updateTaskStatus/${id}/${taskStatus}`,
    //         method: "post",
    //       }).then((res) => {
    //         this.$message({
    //           type: "success",
    //           message: res.msg,
    //           onClose: () => {
    //             this.initData();
    //           },
    //         });
    //       });
    //     })
    //     .catch(() => {});
    // },

    viewTaskLog(id) {
      this.formVisibleLog = true;
        this.$nextTick(() => {
        this.$refs.ViewTaskLogForm.initLoad(id);
      });
    },

    // addOrUpdateHandle(id, isDetail) {
    //   this.formVisible = true;
    //   this.$nextTick(() => {
    //     this.$refs.JNPFForm.init(id, isDetail);
    //   });
    // },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/example/BusTask/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jnpf.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      // this.formVisibleCc = false;
      // this.formVisibleTq = false;
      // this.formVisibleXfzj = false;
      this.formVisibleLog = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.query.taskstatus = "6"
      this.search();
    },
  },
};
</script>
<style>
    .yellow{
        background-color: #FFFFCC;
    }
</style>
